/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.pagequery;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.rrx360.common.util.DateUtil;


/**
 * @author liyuliang
 *
 */
public class PageQueryCondition {

  private long start = -1;

  private int pageSize;

  private long pageIndex;

  private List<FieldEntry> fieldList;

  private Map<String, FieldEntry> fieldMap;

  private List<SortEntry> sortList;

  private Map<String, SortEntry> sortMap;

  public PageQueryCondition() {

  }

  public PageQueryCondition(long pageIndex, int pageSize) {
    this.start = (pageIndex - 1) * pageSize;
    this.pageSize = pageSize;
  }

  public void addSortField(String key, long seq, String order) {
    if (sortList == null) {
      sortList = new ArrayList<SortEntry>();
      sortMap = new HashMap<String, SortEntry>();
    }
    SortEntry sortEntry = new SortEntry(key, seq, order);
    sortList.add(sortEntry);
    sortMap.put(key, sortEntry);
  }


  public long getPageIndex() {
    return pageIndex;
  }

  public void setPageIndex(long pageIndex) {
    this.pageIndex = pageIndex;
  }

  /**
   * Filter field.
   * 
   * @param names
   */
  public void filterField(String[] names) {
    // TODO
  }


  /**
   * Set queryKey to 'name' condition
   * 
   * @param name
   * @param queryKey
   */
  public PageQueryCondition setQueryKey(String name, String queryKey) {
    FieldEntry entry = getField(name);
    if (entry != null) {
      entry.setQueryKey(queryKey);
    }
    return this;
  }

  public PageQueryCondition setQueryCmp(String name, String cmp) {
    FieldEntry entry = getField(name);
    if (entry != null) {
      entry.setQueryCmp(cmp);
    }
    return this;
  }

  public PageQueryCondition setQueryKeyAndCmp(String name, String queryKey, String cmp) {
    return setQueryKeyCmpAndType(name, queryKey, cmp, null);
  }

  public PageQueryCondition setQueryKeyCmpAndType(String name, String queryKey, String cmp,
      Class type) {
    FieldEntry entry = getField(name);
    if (entry != null) {
      entry.setQueryKey(queryKey);
      entry.setQueryCmp(cmp);
      if (type != null) {
        setQueryValueByType(name, type);
      }
    }
    return this;
  }

  public PageQueryCondition setQuery(String name, String queryKey, String cmp, Object value) {
    FieldEntry entry = getField(name);
    if (entry != null) {
      entry.setQueryKey(queryKey);
      entry.setQueryCmp(cmp);
      entry.setQueryValue(value);
    }
    return this;
  }

  /**
   * set queryValue by type
   * 
   * @param name
   * @param type
   * @return
   */
  public PageQueryCondition setQueryValueByType(String name, Class<?> type) {
    FieldEntry entry = getField(name);
    if (entry != null) {
      if (Long.class.equals(type)) {
        if (entry.getValue() == null || "".equals(entry.getValue())
            || entry.getValue() instanceof Long) {
          return this;
        } else if (entry.getValue() instanceof String) {
          entry.setQueryValue(Long.parseLong((String) entry.getValue()));
        }
      } else if (Date.class.equals(type)) {
        if (entry.getValue() instanceof Timestamp) {
          return this;
        }
        String value = (String) entry.getQueryValue();
        if (StringUtils.isEmpty(value)) {
          return this;
        }
        entry.setQueryValue(DateUtil.parseYyyy_MM_dd(value));
      } else if (Boolean.class.equals(type)) {
        if ("1".equals(entry.getValue()) || "true".equals(entry.getValue())) {
          entry.setQueryValue(true);
        } else {
          entry.setQueryValue(false);
        }
      }
    }
    return this;
  }

  public FieldEntry getField(String name) {
    if (fieldMap == null) {
      return null;
    }
    return fieldMap.get(name);
  }

  public <T> T getFieldQueryValue(String name) {
    if (fieldMap == null) {
      return null;
    }
    FieldEntry entry = fieldMap.get(name);
    if (entry == null) {
      return null;
    }
    return (T) entry.getQueryValue();
  }

  public PageQueryCondition putField(FieldEntry entry) {
    if (fieldMap == null) {
      fieldMap = new HashMap<String, FieldEntry>();
    }
    if (fieldList == null) {
      fieldList = new ArrayList<FieldEntry>();
    }
    fieldList.add(entry);
    fieldMap.put(entry.getName(), entry);
    return this;
  }

  public PageQueryCondition removeField(String name) {
    if (fieldMap != null) {
      fieldMap.remove(name);
    }
    if (fieldList != null) {
      int index = -1;
      for (int i = 0; i < fieldList.size(); i++) {
        if (StringUtils.equals(name, fieldList.get(i).getName())) {
          index = i;
          break;
        }
      }
      if (index != -1) {
        fieldList.remove(index);
      }
    }
    return this;
  }

  private SortEntry getSortEntry(String name) {
    if (sortMap == null) {
      return null;
    }
    return sortMap.get(name);
  }

  public PageQueryCondition setSortQueryKey(String name, String queryKey) {
    SortEntry entry = getSortEntry(name);
    if (entry != null) {
      entry.setQueryKey(queryKey);
    }
    return this;
  }

  public long getStart() {
    if (start == -1) {
      start = (pageIndex - 1) * pageSize;
    }
    return start;
  }



  public List<FieldEntry> getFieldList() {
    return fieldList;
  }

  public void setFieldList(List<FieldEntry> fieldList) {
    this.fieldList = fieldList;
  }

  public void setStart(long start) {
    this.start = start;
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }



  public Map<String, FieldEntry> getFieldMap() {
    return fieldMap;
  }

  public void setFieldMap(Map<String, FieldEntry> fieldMap) {
    this.fieldMap = fieldMap;
  }

  public List<SortEntry> getSortList() {
    return sortList;
  }

  public void setSortList(List<SortEntry> sortList) {
    this.sortList = sortList;
  }

  public Map<String, SortEntry> getSortMap() {
    return sortMap;
  }

  public void setSortMap(Map<String, SortEntry> sortMap) {
    this.sortMap = sortMap;
  }

}
